package xyz.jangle.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
/**
 * 比较linkedList 使用迭代器与普通for循环的效率。
 * 比较迭代器与ArrayList的遍历效率。
 * @author Administrator
 *
 */
public class ListMain {

	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		for (int i = 0; i < 39999; i++) {
			list.add(""+i);
			list2.add(""+i);
		}
		Iterator<String> iterator = list.iterator();
		Iterator<String> iterator2 = list2.iterator();
		long time1 = System.currentTimeMillis();
		
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		long time2 = System.currentTimeMillis();

		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		long time3 = System.currentTimeMillis();
		
		for(String str:list2) {
			System.out.println(str);
		}
		long time4 = System.currentTimeMillis();
		while(iterator2.hasNext()) {
			System.out.println(iterator2.next());
		}
		long time5 = System.currentTimeMillis();
		
		
		System.out.println("Iterator:"+(time2-time1));
		System.out.println("LinkedList For:"+(time3-time2));
		System.out.println("ArrayList:"+(time4-time3));
		System.out.println("ArrayList Iterator"+(time5-time4));
	}

}
